home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 05 - 1989 / 05.12 Dec 89 / MacTutor App Code / MacTutorApp.cp next >
Encoding:
Text File  |  1989-10-18  |  7.4 KB  |  329 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #    MacTutorApp
  3. #
  4. #    A rudimentary application skeleton
  5. #    J. Langowski / MacTutor 1989
  6. #------------------------------------------------------------------------------*/
  7. #include <Types.h>
  8. #include <QuickDraw.h>
  9. #include <Fonts.h>
  10. #include <Events.h>
  11. #include <OSEvents.h>
  12. #include <Controls.h>
  13. #include <Windows.h>
  14. #include <Menus.h>
  15. #include <TextEdit.h>
  16. #include <Dialogs.h>
  17. #include <Desk.h>
  18. #include <Scrap.h>
  19. #include <ToolUtils.h>
  20. #include <Memory.h>
  21. #include <SegLoad.h>
  22. #include <Files.h>
  23. #include <OSUtils.h>
  24. #include <Traps.h>
  25. #include <StdLib.h>
  26.  
  27. // Constants, resource definitions, etc.
  28.  
  29. #define kMinSize                48 // min heap needed in K
  30.  
  31. #define    rMenuBar    128                /* application's menu bar */
  32. #define    rAboutAlert    128                /* about alert */
  33. #define    rDocWindow    128                /* application's window */
  34.  
  35. #define    mApple                    128        /* Apple menu */
  36. #define    iAbout                    1
  37.  
  38. #define    mFile                    129        /* File menu */
  39. #define    iNew                    1
  40. #define    iClose                    4
  41. #define    iQuit                    12
  42.  
  43. #define    mEdit                    130        /* Edit menu */
  44. #define    iUndo                    1
  45. #define    iCut                    3
  46. #define    iCopy                    4
  47. #define    iPaste                    5
  48. #define    iClear                    6
  49.  
  50. #define    myMenu                    131        /* Sample menu */
  51. #define    item1                    1
  52. #define    item2                    2
  53. #define    item3                    3
  54. #define    item5                    5
  55.  
  56. #include "TDocument.h"
  57. #include "TApplication.h"
  58. #include "MacTutorApp.h"
  59.  
  60. // create and delete document windows
  61.  
  62. // we pass the resID parameter up to our base class,
  63. // which actually creates the window for us
  64. TMacTutorDocument::TMacTutorDocument(short resID, StringPtr s)    : (resID)
  65. {
  66.     SetDisplayString(s);
  67.     ShowWindow(fDocWindow);        // Make sure the window is visible
  68. }
  69.  
  70. TMacTutorDocument::~TMacTutorDocument(void)
  71. {
  72.     HideWindow(fDocWindow);
  73. }
  74.  
  75. void TMacTutorDocument::DoUpdate(void)
  76. {
  77.     BeginUpdate(fDocWindow);                // this sets up the visRgn 
  78.     if ( ! EmptyRgn(fDocWindow->visRgn) )    // draw if updating needs to be done 
  79.       {
  80.         DrawWindow();
  81.       }
  82.     EndUpdate(fDocWindow);
  83. }
  84.  
  85. // Draw the contents of an application window. 
  86.  
  87. void TMacTutorDocument::DrawWindow(void)
  88. {
  89.     SetPort(fDocWindow);
  90.     EraseRect(&fDocWindow->portRect);
  91.     
  92.     MoveTo(100,100);
  93.     TextSize(18); TextFont(monaco);
  94.     DrawString(fDisplayString);
  95.     
  96. } // DrawWindow
  97.  
  98. // Methods for our application class
  99. TMacTutorApp::TMacTutorApp(void)
  100. {
  101.     Handle    menuBar;
  102.  
  103.     // read menus into menu bar
  104.     menuBar = GetNewMBar(rMenuBar);
  105.     // install menus
  106.     SetMenuBar(menuBar);
  107.     DisposHandle(menuBar);
  108.     // add DA names to Apple menu
  109.     AddResMenu(GetMHandle(mApple), 'DRVR');
  110.     DrawMenuBar();
  111.  
  112.     // create empty mouse region
  113.     fMouseRgn = NewRgn();
  114.     // create a single empty document
  115.     DoNew();
  116. }
  117.  
  118. // Tell TApplication class how much heap we need
  119. long TMacTutorApp::HeapNeeded(void)
  120. {
  121.     return (kMinSize * 1024);
  122. }
  123.  
  124. // Calculate a sleep value for WaitNextEvent. This takes into account the things
  125. // that DoIdle does with idle time.
  126.  
  127. unsigned long TMacTutorApp::SleepVal(void)
  128. {
  129.     unsigned long sleep;
  130.     const long kSleepTime = 0x7fffffff;    // a very large positive number
  131.  
  132.     sleep = kSleepTime;                // default value for sleep
  133.     if ((!fInBackground))
  134.     {
  135.           sleep = GetCaretTime();    // A reasonable time interval for MenuClocks, etc.
  136.     }
  137.     return sleep;
  138. }
  139.  
  140. void TMacTutorApp::AdjustMenus(void)
  141. {
  142.     WindowPtr    frontmost;
  143.     MenuHandle    menu;
  144.     Boolean undo,cutCopyClear,paste;
  145.  
  146.     TMacTutorDocument* fMacTutorCurDoc = (TMacTutorDocument*) fCurDoc;
  147.  
  148.     frontmost = FrontWindow();
  149.  
  150.     menu = GetMHandle(mFile);
  151.     if ( fDocList->NumDocs() < kMaxOpenDocuments )
  152.       EnableItem(menu, iNew);            // New is enabled when we can open more documents 
  153.     else DisableItem(menu, iNew);
  154.     if ( frontmost != (WindowPtr) nil )    // Close is enabled when there is a window to close 
  155.       EnableItem(menu, iClose);
  156.     else DisableItem(menu, iClose);
  157.  
  158.     undo = false;
  159.     cutCopyClear = false;
  160.     paste = false;
  161.     
  162.     if ( fMacTutorCurDoc == nil )
  163.       {
  164.         undo = true;                // all editing is enabled for DA windows 
  165.         cutCopyClear = true;
  166.         paste = true;
  167.       }
  168.       
  169.     menu = GetMHandle(mEdit);
  170.     if ( undo )
  171.         EnableItem(menu, iUndo);
  172.     else
  173.         DisableItem(menu, iUndo);
  174.     
  175.     if ( cutCopyClear )
  176.       {
  177.         EnableItem(menu, iCut);
  178.         EnableItem(menu, iCopy);
  179.         EnableItem(menu, iClear);
  180.       } 
  181.     else
  182.       {
  183.         DisableItem(menu, iCut);
  184.         DisableItem(menu, iCopy);
  185.         DisableItem(menu, iClear);
  186.       }
  187.       
  188.     if ( paste )
  189.         EnableItem(menu, iPaste);
  190.     else
  191.         DisableItem(menu, iPaste);
  192.         
  193.     menu = GetMHandle(myMenu);
  194.         EnableItem(menu, item1);
  195.         EnableItem(menu, item2);
  196.         EnableItem(menu, item3);
  197.         EnableItem(menu, item5);
  198.  
  199.         CheckItem(menu, item1, false);
  200.         CheckItem(menu, item2, false);
  201.         CheckItem(menu, item3, false);
  202.         CheckItem(menu, item5, false);
  203.         CheckItem(menu, fMacTutorCurDoc->GetItemSelected(), true);
  204. } // AdjustMenus
  205.  
  206.  
  207. void TMacTutorApp::DoMenuCommand(short menuID, short menuItem)
  208. {
  209.     short        itemHit;
  210.     Str255        daName;
  211.     short        daRefNum;
  212.     WindowPtr    window;
  213.     TMacTutorDocument* fMacTutorCurDoc = (TMacTutorDocument*) fCurDoc;
  214.  
  215.     window = FrontWindow();
  216.     switch ( menuID )
  217.       {
  218.         case mApple:
  219.             switch ( menuItem )
  220.               {
  221.                 case iAbout:        // About box
  222.                     itemHit = Alert(rAboutAlert, nil);
  223.                     break;
  224.                 default:            // DAs etc.
  225.                     GetItem(GetMHandle(mApple), menuItem, daName);
  226.                     daRefNum = OpenDeskAcc(daName);
  227.                     break;
  228.               }
  229.             break;
  230.         case mFile:
  231.             switch ( menuItem )
  232.               {
  233.                 case iNew:
  234.                     DoNew();
  235.                     break;
  236.                 case iClose:
  237.                     if (fMacTutorCurDoc != nil)
  238.                       {
  239.                         fDocList->RemoveDoc(fMacTutorCurDoc);
  240.                         delete fMacTutorCurDoc;
  241.                       }
  242.                     else CloseDeskAcc(((WindowPeek) fWhichWindow)->windowKind);
  243.                     break;
  244.                 case iQuit:
  245.                     Terminate();
  246.                     break;
  247.               }
  248.             break;
  249.         case mEdit:                    // call SystemEdit for DA editing & MultiFinder 
  250.             if ( !SystemEdit(menuItem-1) )
  251.               {
  252.                 switch ( menuItem )
  253.                   {
  254.                     case iCut:
  255.                         break;
  256.                     case iCopy:
  257.                         break;
  258.                     case iPaste:
  259.                         break;
  260.                     case iClear:
  261.                         break;
  262.                    }
  263.               }
  264.             break;
  265.         case myMenu:
  266.             if (fMacTutorCurDoc != nil) 
  267.             {
  268.                 switch ( menuItem )
  269.                   {
  270.                     case item1:
  271.                         fMacTutorCurDoc->SetDisplayString("\pC++");
  272.                         break;
  273.                     case item2:
  274.                         fMacTutorCurDoc->SetDisplayString("\pSample");
  275.                         break;
  276.                     case item3:
  277.                         fMacTutorCurDoc->SetDisplayString("\pApplication");
  278.                         break;
  279.                     case item5:
  280.                         fMacTutorCurDoc->SetDisplayString("\pHave Fun");
  281.                         break;
  282.                    }
  283.             fMacTutorCurDoc->SetItemSelected(menuItem);
  284.             InvalRect(&(window->portRect));
  285.             fMacTutorCurDoc->DoUpdate();
  286.             }
  287.             break;
  288.       }
  289.     HiliteMenu(0);
  290. } // DoMenuCommand
  291.  
  292. // Create a new document and window. 
  293. void TMacTutorApp::DoNew(void)
  294. {
  295.     TMacTutorDocument* tMacTutorDoc;
  296.     
  297.     tMacTutorDoc = new TMacTutorDocument(rDocWindow,"\pNothing selected yet.");
  298.     // if we didn't get an allocation error, add it to list
  299.     if (tMacTutorDoc != nil)
  300.       fDocList->AddDoc(tMacTutorDoc);
  301. } // DoNew
  302.  
  303. void TMacTutorApp::Terminate(void)
  304. {
  305.     ExitLoop();
  306. } // Terminate
  307.  
  308. // Our application object, initialized in main(). We make it
  309. // global so our functions which don't belong to any class
  310. // can find the active document.
  311. TMacTutorApp *gTheApplication;
  312.  
  313. // main is the entrypoint to the program
  314. int main(void)
  315. {
  316.     // Create our application object. This MUST be the FIRST thing
  317.     // done in main(), since it initializes the Toolbox for us.
  318.     gTheApplication = new TMacTutorApp;
  319.     if (gTheApplication == nil)        // if we couldn't allocate object (impossible!?)
  320.       return 0;                        // go back to Finder
  321.     
  322.     // Start our main event loop running. This won't return until user quits
  323.     gTheApplication->EventLoop();
  324.  
  325.     // We always return a value, like good little ANSI worshippers
  326.     return 0;
  327. }
  328.  
  329.